//
//  JohnSegment.m
//  SCJHAPP_MealNew
//
//  Created by sheldon on 14/12/26.
//  Copyright (c) 2014年 scjh. All rights reserved.
//

#import "JohnSegment.h"
//@protocol JohnSegmentDelegate <NSObject>
//
//
//
//@end

@interface JohnSegment()

@property (nonatomic, strong) UIButton * lastSelected;
@property (nonatomic, assign) NSInteger currentValue;

@property (nonatomic, strong) id<NSObject> target;

@property (nonatomic, assign) UIControlEvents event;

@property (nonatomic, assign) SEL action;

@property (nonatomic, strong) NSMutableArray * segementBtnArray;

@end

@implementation JohnSegment

- (instancetype)initWithItems:(NSArray *)items{

    if (self = [super init]) {
        self.clipsToBounds = YES;
        self.layer.cornerRadius = 5;

         _segementBtnArray = [NSMutableArray array];
        [self setUpUIWithItemArray:items];
        self.bounds = CGRectMake(0, 0, 271, 30);
        self.userInteractionEnabled = YES;
        _selectedSegmentIndex = NSNotFound;
        [self addObserver:self forKeyPath:@"currentValue" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    }
    return self;
}
- (void)setFrame:(CGRect)frame{

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)setUpUIWithItemArray:(NSArray *)items{
    float segeX = 0;
    for (NSString * segeName in items) {
        UIButton * segBtn = [[UIButton alloc] initWithFrame:CGRectMake(segeX, 0, 272/items.count, 30)];
        [segBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        segBtn.tag = [items indexOfObject:segeName];
        segBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [segBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [segBtn setTitle:segeName forState:UIControlStateNormal];
        [segBtn setTitle:segeName forState:UIControlStateSelected];
        [segBtn setBackgroundImage:LOAD_IMAGE(@"Segement_bg.png") forState:UIControlStateSelected];
        [segBtn setBackgroundImage:LOAD_IMAGE(@"Segement_Selected_bg.png") forState:UIControlStateNormal];
        
        [segBtn addTarget:self action:@selector(segBtnCilc:) forControlEvents:UIControlEventTouchUpInside];
        segeX = CGRectGetMaxX(segBtn.frame);
        [_segementBtnArray addObject:segBtn];
        [self addSubview:segBtn];
    }
}
- (void)segBtnCilc:(UIButton *)btn{
    [btn setBackgroundImage:LOAD_IMAGE(@"Segement_bg.png") forState:UIControlStateHighlighted];
    [self.lastSelected setBackgroundImage:LOAD_IMAGE(@"Segement_Selected_bg.png") forState:UIControlStateHighlighted];
   
    if (_lastSelected) {
        if (_lastSelected.tag == btn.tag) {
            return;
        }
        _lastSelected.selected = NO;
        
    }
    
    
    _lastSelected = btn;
    _lastSelected.selected = YES;
    self.currentValue = btn.tag;
    
}

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents{
    
    _target = target;
    _event = controlEvents;
    _action = action;


}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

    if (_event == UIControlEventValueChanged && _target!=nil) {
        NSLog(@"%d",_currentValue);

        [self.target performSelector:self.action withObject:@(_currentValue)];
    }
}

- (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex{

    UIButton * btn = self.segementBtnArray[selectedSegmentIndex];
    [self segBtnCilc:btn];
}
- (void)dealloc{
    [self removeObserver:self forKeyPath:@"currentValue"];
}
@end

